/**
 * 
 */
package com.wubo.sec.web.action;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.wubo.common.data.DataBackupManager;
import com.wubo.common.data.DataRenewManager;
import com.wubo.common.jdbc.SQLExecutor;
import com.wubo.common.util.StringUtils;

import flexjson.JSONSerializer;

/** 系统高级设定
 * @author WuBo
 * @CreateDate 2010-9-27
 * @version 1.0.01
 */
@Controller
public class AdvancedConfigAction extends BaseSecAction {
	@Resource
	private SQLExecutor sqlExecutor;
	@Resource
	private DataBackupManager dataBackupManager;
	@Resource
	private DataRenewManager dataRenewManager;
	
	private String tableName; //表名
	private String selects; //查询语句
	private String tables; //表名
	private String encoding = "UTF-8"; //编码
	
	private boolean clearBeforeInsert; //恢复数据前清空表
	
	private boolean skipError; //遇到错误时跳过继续执行
	
	private File filename;
	
	public String getSelects() {
		return selects;
	}

	public void setSelects(String selects) {
		this.selects = selects;
	}

	public String getTables() {
		return tables;
	}

	public void setTables(String tables) {
		this.tables = tables;
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	public File getFilename() {
		return filename;
	}

	public void setFilename(File filename) {
		this.filename = filename;
	}

	public boolean isClearBeforeInsert() {
		return clearBeforeInsert;
	}

	public void setClearBeforeInsert(boolean clearBeforeInsert) {
		this.clearBeforeInsert = clearBeforeInsert;
	}

	public boolean isSkipError() {
		return skipError;
	}

	public void setSkipError(boolean skipError) {
		this.skipError = skipError;
	}

	/**
	 * 根据表名得到所有列
	 */
	public void showColumn(){
		try{
			if(StringUtils.hasText(tableName)){
				String[] columnNames = (String[]) sqlExecutor.execute(new SQLExecutor.ConnectionCallback(){
					public Object execute(Connection conn) throws SQLException {
						java.sql.Statement stmt = conn.createStatement();
						stmt.setMaxRows(2);
						stmt.setFetchSize(1);
						java.sql.ResultSet rs = stmt.executeQuery("select * from "+tableName);
						java.sql.ResultSetMetaData rsmd = rs.getMetaData();
						int count = rsmd.getColumnCount();
						String[] columnNames = new String[count];
						for(int i=0; i<count; i++){
							columnNames[i] = rsmd.getColumnName(i+1);
						}
						return columnNames;
					}
				});
				jsonSuccess(new JSONSerializer().serialize(columnNames), true);
			}
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(t.getMessage()); //把错误信息反馈给用户
		}
	}
	/**
	 * 数据备份
	 */
	public void backup(){
		try{
			String[] SQL = selects.split(";");
			String[] backupTables = tables.split(";");
			ServletActionContext.getResponse().setContentType("application/octet-stream");
			ServletActionContext.getResponse().setCharacterEncoding(encoding);
			ServletActionContext.getResponse().setHeader("Content-disposition", "attachment; filename=SecurityFramwork.backup"); 
			dataBackupManager.backup(SQL, backupTables, ServletActionContext.getResponse().getOutputStream(), encoding);
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(t.getMessage()); //把错误信息反馈给用户
		}
	}
	/**
	 * 从备份文件恢复数据
	 */
	public void renew(){
		try{
			dataRenewManager.renew(new java.io.FileInputStream(filename), clearBeforeInsert, skipError, encoding);
			
			jsonSuccess(null);
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(t.getMessage()); //把错误信息反馈给用户
		}
	}
}
